﻿Imports System.Runtime.InteropServices
Imports Microsoft.Xna.Framework.Input
Public Class Form1
    Dim isCellphone As Boolean
    Dim helper As SimulateInput = New SimulateInput()
    Dim state = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One)
    Public MoveMouseMultiplier As Double = 40.0
    Const MouseGain As Double = 1.0
    Dim cursorMove = state.ThumbSticks.Left
    Dim x As Double = cursorMove.X
    Dim y As Double = cursorMove.Y
    Dim velocity As Double = Math.Sqrt(x ^ 2 + y ^ 2)
    Dim speed As Double
    Dim hitWallVibrate As Double
    Dim motorSpeedVirbrate As Double

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        isCellphone = False
        Me.Hide()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'Mouse Move Multiplier is Cursor Speed, Default = 40.0

        'MouseGain is Cursor Directional Move Tolerance bigger number equals less tolerance
        Const MouseGain As Double = 1.0        'Must be an odd number
        Const SpeedUpdate As Double = 0.01

        'helper.SetMouseButtonState(Windows.Forms.MouseButtons.Left, True)
        'helper.SetMouseButtonState(Windows.Forms.MouseButtons.Left, False)

        Dim state = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One)

        Dim cursorMove = state.ThumbSticks.Left

        Dim x As Double = cursorMove.X
        Dim y As Double = cursorMove.Y
        Dim velocity As Double = Math.Sqrt(x ^ 2 + y ^ 2)

        Dim hitWallVibrate As Double
        Dim motorSpeedVirbrate As Double

        If isCellphone Then
            x = ((x + 1.0) / 2.0)
            y = 1.0 - ((y + 1.0) / 2.0)

            'Dim button1 = DistanceFrom(New PointF(x, y), New PointF(0.0, 0.0))
            'Dim button2 = DistanceFrom(New PointF(x, y), New PointF(0.5, 0.0))
            'Dim button3 = DistanceFrom(New PointF(x, y), New PointF(1.0, 0.0))
            'Dim button4 = DistanceFrom(New PointF(x, y), New PointF(0.0, 0.5))
            'Dim button5 = DistanceFrom(New PointF(x, y), New PointF(0.5, 0.5))
            'Dim button6 = DistanceFrom(New PointF(x, y), New PointF(1.0, 0.5))
            'Dim button7 = DistanceFrom(New PointF(x, y), New PointF(0.0, 1.0))
            'Dim button8 = DistanceFrom(New PointF(x, y), New PointF(0.5, 1.0))
            'Dim button9 = DistanceFrom(New PointF(x, y), New PointF(1.0, 1.0))

            'Dim currentButton As Integer = -1

            'If button1 <= 0.2 Then
            '    currentButton = 1
            'End If
            'If button2 <= 0.1 Then
            '    currentButton = 2
            'End If
            'If button3 <= 0.2 Then
            '    currentButton = 3
            'End If
            'If button4 <= 0.1 Then
            '    currentButton = 4
            'End If
            'If button5 <= 0.2 Then
            '    currentButton = 5
            'End If
            'If button6 <= 0.1 Then
            '    currentButton = 6
            'End If
            'If button7 <= 0.2 Then
            '    currentButton = 7
            'End If
            'If button8 <= 0.1 Then
            '    currentButton = 8
            'End If
            'If button9 <= 0.2 Then
            '    currentButton = 9
            'End If

            'Label1.Text = button1
            'Label2.Text = button2
            'Label3.Text = button3
            'Label4.Text = button4
            'Label5.Text = button5
            'Label6.Text = button6
            'Label7.Text = button7
            'Label8.Text = button8
            'Label9.Text = button9

            'Me.Text = currentButton

            'Dim StateA As Boolean = state.IsButtonDown(Buttons.A)

            'If currentButton <> cellLastSelectedButton And StateA Then
            '    cellLastSelectedButtonCount = 0
            'End If

            'If (Environment.TickCount - cellLastPressedButtonTime) < 1000 Then
            '    Label10.ForeColor = Color.Gray
            'Else
            '    Label10.ForeColor = Color.Black

            '    If cellLastSelectedButton >= 0 And cellLastSelectedButtonCount > 0 Then
            '        SendKeys.Send(buttonValues(cellLastSelectedButton - 1).Substring(cellLastSelectedButtonCount - 1, 1))
            '    End If

            '    cellLastSelectedButton = -1
            'End If

            'If StateA And (Not cellWasAPressedLast) And currentButton <> -1 Then
            '    If cellLastSelectedButton = currentButton Then
            '        If cellLastPressedButton = currentButton And (Environment.TickCount - cellLastPressedButtonTime) < 1000 Then
            '            'Update current char
            '            Dim wrapAroundAmount As Integer = buttonValues(currentButton - 1).Length

            '            cellLastSelectedButtonCount = (cellLastSelectedButtonCount + 1) Mod wrapAroundAmount

            '            Label10.Text = Label10.Text.Substring(0, Label10.Text.Length - 1) & buttonValues(currentButton - 1).Substring(cellLastSelectedButtonCount, 1)
            '        Else
            '            'Add new char
            '            Label10.Text = Label10.Text & buttonValues(currentButton - 1).Substring(0, 1)
            '            cellLastSelectedButtonCount = 0
            '        End If

            '        cellLastPressedButton = currentButton
            '        cellLastPressedButtonTime = Environment.TickCount
            '    Else
            '        If cellLastSelectedButton >= 0 Then
            '            SendKeys.Send(buttonValues(cellLastSelectedButton - 1).Substring(cellLastSelectedButtonCount, 1))
            '        End If

            '        cellLastSelectedButton = -1
            '    End If
            'End If

            'Dim StateB As Boolean = state.IsButtonDown(Buttons.B)

            'If StateB And (Not cellWasBPressedLast) Then
            '    If Label10.Text.Length > 0 Then
            '        Label10.Text = Label10.Text.Substring(0, Label10.Text.Length - 1)
            '        cellLastPressedButtonTime = 0
            '    End If
            'End If

            'cellWasBPressedLast = StateB
            'cellWasAPressedLast = StateA
            'cellLastSelectedButton = currentButton
        Else

            x = x ^ MouseGain
            y = y ^ MouseGain

            speed = (speed * (1.0 - SpeedUpdate)) + (velocity / Math.Sqrt(2) * SpeedUpdate)

            'x = x * speed
            'y = y * speed

            Dim currentSpeed As Double = 0.2

            If Math.Abs(x) >= 0.9 Or Math.Abs(y) < 0.9 Then
                currentSpeed = 1.0
            End If

            speed = (speed * 0.95) + (currentSpeed * 0.05)

            x *= speed
            y *= speed


            motorSpeedVirbrate = velocity * speed / 5

            If helper.MoveMouse(x * MoveMouseMultiplier, y * -MoveMouseMultiplier) Then
                hitWallVibrate = Math.Abs(x)
            Else
                hitWallVibrate = 0.0

                'If x < 0.0 Then
                '    GamePad.SetVibration(Microsoft.Xna.Framework.PlayerIndex.One, Math.Abs(x), 0.0)
                'Else
                '    GamePad.SetVibration(Microsoft.Xna.Framework.PlayerIndex.One, 0.0, Math.Abs(x))
                'End If

                'GamePad.SetVibration(Microsoft.Xna.Framework.PlayerIndex.One, (velocity ^ 2) / 8, (velocity ^ 2) / 8)
            End If

            '''' 'If' Statement below assigns Xbox controller butons to the Left Mouse Click
            

            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.B) Then
                DoKeyBoard(0, Keys.Escape)  ' key down is 0 - no flag...
                DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Escape)
            End If

            Dim Dpadup As Integer = -1
            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadUp) Then
                DoKeyBoard(0, Keys.Up)  ' key down is 0 - no flag...
                DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Up)
            End If
            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadLeft) Then
                DoKeyBoard(0, Keys.Left)  ' key down is 0 - no flag...
                DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Left)
            End If
            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadRight) Then
                DoKeyBoard(0, Keys.Right)  ' key down is 0 - no flag...
                DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Right)
            End If
            Dim c As Integer = -1
            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadDown) Then
                DoKeyBoard(0, Keys.Down)  ' key down is 0 - no flag...
                DoKeyBoard(NativeMethods.KEYEVENTF.KEYUP, Keys.Down)

                'Do Until c = 1
                '    timerwait.Start()
                'Loop
                'timerwait.Stop()
                'c = -1
            End If

            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.BigButton) Then
                Me.ContextMenuStrip2.Show()
            End If
            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.LeftStick) Or state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.LeftTrigger) Or state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.A) Then
                helper.SetMouseButtonState(Windows.Forms.MouseButtons.Left, True)
            Else
                helper.SetMouseButtonState(Windows.Forms.MouseButtons.Left, False)
            End If

            If state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.RightStick) Or state.IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.RightTrigger) Then
                helper.SetMouseButtonState(Windows.Forms.MouseButtons.Right, True)
            Else
                helper.SetMouseButtonState(Windows.Forms.MouseButtons.Right, False)
            End If
        End If


        GamePad.SetVibration(Microsoft.Xna.Framework.PlayerIndex.One, hitWallVibrate, motorSpeedVirbrate)
    End Sub


    Private Shared Function DistanceFrom(ByVal pt As PointF, ByVal pt2 As PointF) As Double
        Return Math.Sqrt(Math.Pow((pt.X - pt2.X), 2) + Math.Pow((pt.Y - pt2.Y), 2)) / Math.Sqrt(2)
    End Function

    Private Sub btnFB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Feedback.ShowDialog()
    End Sub

    Private Sub OpenDialogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenDialogToolStripMenuItem.Click
        Me.Show()
    End Sub

    Private Sub FeedbackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FeedbackToolStripMenuItem.Click
        Feedback.Show()
    End Sub

    Private Sub ExitApplicationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitApplicationToolStripMenuItem.Click
        End
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        About.Show()
    End Sub

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Me.Hide()
    End Sub







    Private Sub DoKeyBoard(ByVal flags As NativeMethods.KEYEVENTF, ByVal key As Keys)
        Dim input As New NativeMethods.INPUT
        Dim ki As New NativeMethods.KEYBDINPUT
        input.dwType = NativeMethods.InputType.Keyboard
        input.ki = ki
        input.ki.wVk = Convert.ToInt16(key)
        input.ki.wScan = 0
        input.ki.time = 0
        input.ki.dwFlags = flags
        input.ki.dwExtraInfo = IntPtr.Zero
        Dim cbSize As Integer = Marshal.SizeOf(GetType(NativeMethods.INPUT))
        Dim result As Integer = NativeMethods.SendInput(1, input, cbSize)
        If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)
    End Sub


    Private Sub timerwait_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timerwait.Tick

    End Sub
End Class
Public Class NativeMethods

    <DllImport("user32.dll", SetLastError:=True)> _
    Friend Shared Function SendInput(ByVal cInputs As Int32, ByRef pInputs As INPUT, ByVal cbSize As Int32) As Int32
    End Function

    <StructLayout(LayoutKind.Explicit, pack:=1, Size:=28)> _
    Friend Structure INPUT
        <FieldOffset(0)> Public dwType As InputType
        <FieldOffset(4)> Public mi As MOUSEINPUT
        <FieldOffset(4)> Public ki As KEYBDINPUT
        <FieldOffset(4)> Public hi As HARDWAREINPUT
    End Structure

    <StructLayout(LayoutKind.Sequential, pack:=1)> _
    Friend Structure MOUSEINPUT
        Public dx As Int32
        Public dy As Int32
        Public mouseData As Int32
        Public dwFlags As MOUSEEVENTF
        Public time As Int32
        Public dwExtraInfo As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential, pack:=1)> _
    Friend Structure KEYBDINPUT
        Public wVk As Int16
        Public wScan As Int16
        Public dwFlags As KEYEVENTF
        Public time As Int32
        Public dwExtraInfo As IntPtr
    End Structure

    <StructLayout(LayoutKind.Sequential, pack:=1)> _
    Friend Structure HARDWAREINPUT
        Public uMsg As Int32
        Public wParamL As Int16
        Public wParamH As Int16
    End Structure

    Friend Enum InputType As Integer
        Mouse = 0
        Keyboard = 1
        Hardware = 2
    End Enum

    <Flags()> _
    Friend Enum MOUSEEVENTF As Integer
        MOVE = &H1
        LEFTDOWN = &H2
        LEFTUP = &H4
        RIGHTDOWN = &H8
        RIGHTUP = &H10
        MIDDLEDOWN = &H20
        MIDDLEUP = &H40
        XDOWN = &H80
        XUP = &H100
        VIRTUALDESK = &H400
        WHEEL = &H800
        ABSOLUTE = &H8000
    End Enum

    <Flags()> _
    Friend Enum KEYEVENTF As Integer
        EXTENDEDKEY = 1
        KEYUP = 2
        [UNICODE] = 4
        SCANCODE = 8
    End Enum
End Class
